﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using University.Workshop.Model;
using System.Collections.ObjectModel;

namespace University.Workshop.DataAccess
{
    public class UpdateAdministrator: IDisposable
    {
        const string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=DataBaseHotel;Integrated Security=True";
        SqlConnection conex;
        public Administrator GetOperatorByUserName(string userName,string password) 
        {
            Administrator admi;
            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = @"SELECT * FROM [DataBaseHotel].[dbo].[Administrator] WHERE  UserName = @UserName and Password = @Password";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@UserName",userName);
            cmd.Parameters.AddWithValue("@Password",password);

            try
            {
                conex.Open();
                SqlDataReader read = cmd.ExecuteReader();
                if (!read.HasRows)
                {
                    return null;
                }
                read.Read();
                admi = new Administrator { Name = read["FullName"].ToString()
                                          ,Surname = read["Surname"].ToString()
                                          ,UserName = read["UserName"].ToString()
                                          ,Password = read["Password"].ToString()
                                          ,Email = read["Email"].ToString()};
                return admi;
            }
            catch ( Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
        }
        public void UpdateByUserName(string userName,string password,Administrator administrator)
        {
            if (administrator == null)
                throw new ArgumentException("Invalid Input Parameters");

            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = @"UPDATE [DataBaseHotel].[dbo].[Administrator]
                                   SET [UserName] = @UserName
                                      ,[Password] = @Password
                                      ,[FullName] = @FullName
                                      ,[Surname] = @Surname
                                      ,[Email] = @Email
                                WHERE UserName = @UserNameActual and Password = @PasswordActual";
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@UserName", administrator.UserName);
            cmd.Parameters.AddWithValue("@Password", administrator.Password);
            cmd.Parameters.AddWithValue("@FullName", administrator.Name);
            cmd.Parameters.AddWithValue("@Surname", administrator.Surname);
            cmd.Parameters.AddWithValue("@Email", administrator.Email);
            cmd.Parameters.AddWithValue("@UserNameActual",userName);
            cmd.Parameters.AddWithValue("@PasswordActual",password);
            try
            {
                conex.Open();
                cmd.ExecuteNonQuery();
               
            }
            catch (Exception e)
            {
                throw new ArgumentException("Error creating a user", e.Message);
            }
        }
        public void GetAllAdministrators(DataGridView administrator) 
        {
            if (administrator == null)
                throw new ArgumentException("Invalid Input Parameters");

            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = "SELECT * FROM [DataBaseHotel].[dbo].[Administrator]";
            cmd.CommandType = CommandType.Text;
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            SqlDataReader reader = cmd.ExecuteReader();
            int index = 0;
            try
            {
                while (reader.Read())
                {
                    administrator.Rows.Add();
                    administrator["UserName",index].Value = reader["UserName"].ToString();
                    administrator["Password", index].Value = reader["Password"].ToString();
                    administrator["FullName", index].Value = reader["FullName"].ToString();
                    administrator["Surname", index].Value = reader["Surname"].ToString();
                    administrator["Email", index].Value = reader["Email"].ToString();
                    ++index;
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            if (cmd.Connection.State == ConnectionState.Open)
            {
                cmd.Connection.Close();
            }
        }
       
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (conex != null)
                {
                    conex.Dispose();
                    conex = null;
                }
            }
        }
    }
}
